home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / 3.3 - MDEF / Tester.c < prev    next >
C/C++ Source or Header  |  1991-08-20  |  5KB  |  214 lines

  1. /************************************************************/
  2. /*                                                            */
  3. /*    MDEF Tester Code from Chapter Three of                    */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. #define BASE_RES_ID            400
  15. #define APPLE_MENU_ID        400
  16. #define NIL_POINTER            0L
  17. #define MOVE_TO_FRONT        (WindowPtr)-1L
  18. #define REMOVE_ALL_EVENTS    0
  19.  
  20. #define    WNE_TRAP_NUM        0x60
  21. #define    UNIMPL_TRAP_NUM        0x9F
  22. #define MIN_SLEEP            60L
  23. #define    NIL_MOUSE_REGION    0L
  24.  
  25. #define FILE_MENU_ID        401
  26. #define F_QUIT_ITEM            1
  27.  
  28. #define PICT_MENU_ID        403
  29.  
  30.  
  31. Boolean         gDone, gWNEImplemented;
  32. EventRecord    gTheEvent;
  33. MenuHandle    gAppleMenu;
  34. PicHandle    gCurPicture;
  35. WindowPtr    gTheWindow;
  36.  
  37.  
  38. main()
  39. {
  40.     ToolBoxInit();
  41.     MenuBarInit();
  42.     
  43.     gTheWindow = GetNewWindow( BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT );
  44.     SetPort( gTheWindow );
  45.     ShowWindow( gTheWindow );
  46.     
  47.     gCurPicture = GetPicture( BASE_RES_ID );
  48.     
  49.     MainLoop();
  50. }
  51.  
  52.  
  53. /*********************************** ToolBoxInit */
  54.  
  55. ToolBoxInit()
  56. {
  57.     InitGraf( &thePort );
  58.     InitFonts();
  59.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  60.     InitWindows();
  61.     InitMenus();
  62.     TEInit();
  63.     InitDialogs( NIL_POINTER );
  64.     InitCursor();
  65. }
  66.  
  67.  
  68. /***********************************    MenuBarInit    */
  69.  
  70. MenuBarInit()
  71. {
  72.     Handle        myMenuBar;
  73.  
  74.     myMenuBar = GetNewMBar( BASE_RES_ID );
  75.     SetMenuBar( myMenuBar );
  76.     gAppleMenu = GetMHandle( APPLE_MENU_ID );
  77.     AddResMenu( gAppleMenu, 'DRVR' );
  78.     DrawMenuBar();
  79. }
  80.  
  81.  
  82. /******************************** MainLoop *********/
  83.  
  84. MainLoop()
  85. {
  86.     gDone = FALSE;
  87.     gWNEImplemented = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap ) !=
  88.                         NGetTrapAddress( UNIMPL_TRAP_NUM, ToolTrap ) );
  89.     while ( gDone == FALSE )
  90.     {
  91.         HandleEvent();
  92.     }
  93. }
  94.  
  95.  
  96. /************************************* HandleEvent     */
  97.  
  98. HandleEvent()
  99. {
  100.     char    theChar;
  101.     
  102.     if ( gWNEImplemented )
  103.         WaitNextEvent( everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION );
  104.     else
  105.     {
  106.         SystemTask();
  107.         GetNextEvent( everyEvent, &gTheEvent );
  108.     }
  109.     
  110.     switch ( gTheEvent.what )
  111.     {
  112.         case mouseDown: 
  113.             HandleMouseDown();
  114.             break;
  115.         case keyDown:
  116.         case autoKey:
  117.             theChar = gTheEvent.message & charCodeMask;
  118.             if (( gTheEvent.modifiers & cmdKey ) != 0) 
  119.                 HandleMenuChoice( MenuKey( theChar ) );
  120.             break;
  121.         case updateEvt:
  122.             BeginUpdate( (WindowPtr)gTheEvent.message );
  123.             DrawMyPicture( gCurPicture, gTheWindow );
  124.             EndUpdate( (WindowPtr)gTheEvent.message );
  125.             break;
  126.     }
  127. }
  128.  
  129.  
  130. /************************************* HandleMouseDown */
  131.  
  132. HandleMouseDown()
  133. {
  134.     WindowPtr    whichWindow;
  135.     short int    thePart;
  136.     long int    menuChoice, windSize;
  137.     
  138.     thePart = FindWindow( gTheEvent.where, &whichWindow );
  139.     switch ( thePart )
  140.     {
  141.         case inMenuBar:
  142.             menuChoice = MenuSelect( gTheEvent.where );
  143.             HandleMenuChoice( menuChoice );
  144.             break;
  145.         case inSysWindow : 
  146.             SystemClick( &gTheEvent, whichWindow );
  147.             break;
  148.         case inDrag : 
  149.             DragWindow( whichWindow, gTheEvent.where, &(screenBits.bounds) );
  150.             break;
  151.     }
  152. }
  153.  
  154.  
  155. /************************************* HandleMenuChoice */
  156.  
  157. HandleMenuChoice( menuChoice )
  158. long int    menuChoice;
  159. {
  160.     int    theMenu;
  161.     int    theItem;
  162.     
  163.     if ( menuChoice != 0 )
  164.     {
  165.         theMenu = HiWord( menuChoice );
  166.         theItem = LoWord( menuChoice );
  167.         switch ( theMenu )
  168.         {
  169.             case FILE_MENU_ID :
  170.                 if ( theItem == F_QUIT_ITEM )
  171.                     gDone = TRUE;
  172.                 break;
  173.             case PICT_MENU_ID :
  174.                 EraseRect( &gTheWindow->portRect );
  175.                 InvalRect( &gTheWindow->portRect );
  176.                 gCurPicture = GetPicture( BASE_RES_ID + theItem - 1 );
  177.                 break;
  178.         }
  179.         HiliteMenu( 0 );
  180.     }
  181. }
  182.  
  183.  
  184. /******************************** DrawMyPicture *********/
  185.  
  186. DrawMyPicture( thePicture, pictureWindow )
  187. PicHandle    thePicture;
  188. WindowPtr    pictureWindow;
  189. {
  190.     Rect    myRect;
  191.     
  192.     myRect = pictureWindow->portRect;
  193.     CenterPict( thePicture, &myRect );
  194.     DrawPicture( thePicture, &myRect );
  195. }
  196.  
  197.  
  198. /******************************** CenterPict *********/
  199.  
  200. CenterPict( thePicture, myRectPtr )
  201. PicHandle    thePicture;
  202. Rect        *myRectPtr;
  203. {
  204.     Rect    windRect, pictureRect;
  205.     
  206.     windRect = *myRectPtr;
  207.     pictureRect = (**( thePicture )).picFrame;
  208.     myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top))
  209.         / 2 + windRect.top;
  210.     myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
  211.     myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right - pictureRect.left))
  212.         / 2 + windRect.left;
  213.     myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
  214. }